import { parse } from 'qs';
import { login, userInfo, logout } from '../services/app';

export default {
    namespace: 'app',
    state: {
        login: false,
        loading: false,
        user: {
            name: '吴彦祖'
        },
        menu: [
            {
                name: 'start',
                code: 'start',
                url: '/start',
                ico: 'user',
                children: [
                    {
                        name: 'login',
                        code: 'login',
                        url: 'login',
                        ico: 'list',
                    }
                ]
            },
            {
                name: 'home',
                code: 'home',
                url: '/home',
                ico: 'appstore-o',
                children: [
                    {
                        name: '用户',
                        code: 'menu-0-1',
                        url: 'home/users',
                        ico: 'meh-o',
                    },
                    {
                        name: 'flex测试',
                        code: 'menu-0-2',
                        url: 'flex',
                        ico: 'meh-o',
                    },
                    {
                        name: 'dashboard',
                        code: 'menu-0-3',
                        url: 'home/dashboard',
                        ico: 'home',
                    },
                ],
            },
            {
                name: 'user',
                code: 'menu-1',
                url: 'home/users',
                ico: 'meh',
            },
            {
                name: '菜单-2',
                code: 'menu-2',
                url: null,
                ico: 'appstore-o',
                children: [
                    {
                        name: '菜单-2-0',
                        code: 'menu-2-0',
                        url: null,
                        ico: 'meh-o',
                    },
                    {
                        name: '菜单-2-1',
                        code: 'menu-2-1',
                        url: null,
                        ico: 'meh-o',
                    },
                ],
            },
        ],
        loginButtonLoading: false,
    },
    subscriptions: {
        /*setup(args) {
            const { dispatch, history } = args;
            history.listen(location => {
                if (location.pathname === '/') {
                    dispatch(routerRedux.push({
                        pathname: '/login',
                    }));
                }
            });
        }*/
    },
    effects: {
        *login({ payload }, { call, put }) {
            yield put({ type: 'showLoginButtonLoading' });
            const data = yield call(login, parse(payload));
            if (data.success) {
                yield put({
                    type: 'loginSuccess',
                    payload: {
                        data
                    }
                });
            } else {
                yield put({
                    type: 'loginFail',
                    payload: {
                        data
                    }
                });
            }
        },
        *queryUser({ payload }, { call, put }) {
            yield put({ type: 'showLoading' });
            const data = yield call(userInfo, parse(payload));
            if (data.success) {
                yield put({
                    type: 'loginSuccess',
                    payload: {
                        user: {
                            name: data.username
                        }
                    }
                });
            } else {
                yield put({ type: 'hideLoading' });
            }
        },
        *logout({ payload }, { call, put }) {
            const data = yield call(logout, parse(payload));
            if (data.success) {
                yield put({
                    type: 'logoutSuccess'
                });
            }
        },
    },
    reducers: {
        loginSuccess(state, action) {
            return {
                ...state,
                ...action.payload,
                login: true,
                loginButtonLoading: false
            };
        },
        logoutSuccess(state) {
            return {
                ...state,
                login: false
            };
        },
        loginFail(state) {
            return {
                ...state,
                login: false,
                loginButtonLoading: false
            };
        },
        showLoginButtonLoading(state) {
            return {
                ...state,
                loginButtonLoading: true
            };
        },
        showLoading(state) {
            return {
                ...state,
                loading: true
            };
        },
        hideLoading(state) {
            return {
                ...state,
                loading: false
            };
        },
    }
};
